package com.wxgo.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.wxgo.common.model.News;

public class ExcelUtil {

	// 处理2007之前的excel
	public static List<News> readOldExcel(HSSFWorkbook hssfWorkbook) {
		List<News> users = new ArrayList<News>();
		HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
		HSSFCell cell = null;
		HSSFRow row = null;
		for (int i = sheetAt.getFirstRowNum(); i < sheetAt.getPhysicalNumberOfRows(); i++) {
			row = sheetAt.getRow(i);
			if (row == null) {
				continue;
			}
			Object[] objects = new Object[row.getLastCellNum()];
			for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
				cell = row.getCell(j);
				switch (cell.getCellTypeEnum()) {
				case STRING:
					objects[j] = cell.getStringCellValue();
					System.out.println(cell.getStringCellValue());
					break;
				case _NONE:
					objects[j] = "";
					break;
				case BOOLEAN:
					objects[j] = cell.getBooleanCellValue();
					System.out.println(cell.getBooleanCellValue());
					break;
				case NUMERIC:
					// 处理double类型的 1.0===》1
					DecimalFormat df = new DecimalFormat("0");
					String s = df.format(cell.getNumericCellValue());
					objects[j] = s;
					System.out.println(s);
					break;
				default:
					objects[j] = cell.toString();
				}
			}
			// 处理数据
			if (objects != null) {
				News news = new News();
				news.setNewsname((String) objects[0]);
				news.setOutprice(0D);
				news.setVipprice(0D);
				news.setOuttype(1);
				news.setPcode(1);
				news.setProvince("");
				news.setRemark("");
				news.setSite("");
			}
		}
		return users;
	}

	// 处理2007之后的excel
	private List<News> readExcel(XSSFWorkbook xssfWorkbook) {
		List<News> users = new ArrayList<News>();
		// 获得excel第一个工作薄
		XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
		// 行
		XSSFRow row = null;
		// 列
		XSSFCell cell = null;
		for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
			// 获取每一行
			row = sheet.getRow(i);
			// 判断是否出现空行
			if (row == null) {
				continue;
			}
			Object[] objects = new Object[row.getLastCellNum()];
			for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
				cell = row.getCell(j);
				if (cell == null) {
					continue;
				}
				// 第一行数据
				switch (cell.getCellTypeEnum()) {
				case STRING:
					objects[j] = cell.getStringCellValue();
					System.out.println(cell.getStringCellValue());
					break;
				case _NONE:
					objects[j] = "";
					break;
				case BOOLEAN:
					objects[j] = cell.getBooleanCellValue();
					System.out.println(cell.getBooleanCellValue());
					break;
				case NUMERIC:
					// 处理double类型的 1.0===》1
					DecimalFormat df = new DecimalFormat("0");
					String s = df.format(cell.getNumericCellValue());
					objects[j] = s;
					System.out.println(s);
					break;
				default:
					objects[j] = cell.toString();
				}

			}
			// 处理数据
			if (objects != null) {
				News news = new News();
				news.setNewsname((String) objects[0]);
				news.setOutprice(0D);
				news.setVipprice(0D);
				news.setOuttype(1);
				news.setPcode(1);
				news.setProvince("");
				news.setRemark("");
				news.setSite("");
			}

		}
		return users;
	}

}
